package com.source.lightfowl.basedata.controller;
import java.io.IOException;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.source.lightfowl.DTO.ToMarketDownloadDTO;
import com.source.lightfowl.VO.ToMarketCurveVO;
import com.source.lightfowl.basedata.entity.ToMarket;
import com.source.lightfowl.basedata.service.ToMarketService;
import com.source.lightfowl.listener.UploadExcelListener;
import com.source.lightfowl.utils.MyResult;
import com.source.lightfowl.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: cxy
 * @Description: 去向市场前端控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/toMarket")
@Api(value = "去向市场管理", tags = "去向市场管理")
public class ToMarketController {

	@Autowired
	private ToMarketService toMarketService;

	/**
	 * 上传图片
	 * @param file
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "上传二维码图片")
	@PostMapping("/upload")
	public MyResult create(@RequestParam("file") MultipartFile file, HttpServletRequest request){
		String catalogue = toMarketService.upload(file, request);
		return MyResult.build(0,"成功", catalogue);
	}

	/**
	 * 下载Excel
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "下载Excel")
	@GetMapping("/downloadExcel")
	public MyResult download(HttpServletResponse response) throws IOException {
		DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
		downloadExcelHandler.download(response, toMarketService, ToMarketDownloadDTO.class);
		return new MyResult(0, "success");
	}

	/**
	 * 文件下载并且失败的时候返回json（默认失败了会返回一个有部分数据的Excel）
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "文件下载并且失败的时候返回json（默认失败了会返回一个有部分数据的Excel）")
	@GetMapping("/downloadFailedUsingJson")
	public MyResult downloadFailedUsingJson(HttpServletResponse response) throws IOException {
		DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
		downloadExcelHandler.downloadFailedUsingJson(response, toMarketService, ToMarketDownloadDTO.class);
		return new MyResult(0, "success");
	}

	/**
	 * 上传Excel
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "上传Excel")
	@PostMapping("/uploadExcel")
	@ResponseBody
	public MyResult upload(MultipartFile file, HttpServletRequest request) throws IOException {
		EasyExcel.read(file.getInputStream(), ToMarketDownloadDTO.class, new UploadExcelListener(toMarketService, request)).sheet().doRead();
		return new MyResult(0, "success");
	}

	/**
	 * 返回全部列表
	 * @return
	 */
	@ApiOperation(value = "查询所有市场去向信息", notes = "- 只查询审核通过的数据")
	@GetMapping("/findAll")
	public List<ToMarket> findAll(){
		return toMarketService.findAll(1);
	}


	/**
	 * 返回全部列表-分页
	 * @return
	 */
	@ApiOperation(value = "查询所有市场去向信息-分页", notes = "- 只查询审核通过的数据")
	@GetMapping("/findAllPage")
	public MyResult findAllPage(Integer page, Integer rows){
		Page<ToMarket> pageList = toMarketService.findAllPage(1, page, rows);
		return new MyResult(0,"查询成功",  new PageResult<ToMarket>(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据去向市场id查询信息", notes = "查询所有状态的数据")
	@GetMapping(value="/id")
	public MyResult findById(String id){
		return new MyResult(0,"查询成功",toMarketService.findById(id));
	}


	/**
	 * 增加
	 * @param toMarket
	 * @return
	 */
	@ApiOperation(value = "增加去向市场信息", notes = "- 等待管理员审核")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public MyResult add(ToMarket toMarket, MultipartFile markCodeFile, MultipartFile stallCodeFile, HttpServletRequest request){
		toMarket.setState(2);
		toMarketService.save(toMarket, markCodeFile, stallCodeFile, request);
		return new MyResult(0, "新增成功");
	}

	/**
	 * 修改
	 * @param toMarket
	 * @return
	 */
	@ApiOperation(value = "修改去向市场信息")
	@PostMapping("/update")
	public MyResult update(ToMarket toMarket, MultipartFile markCodeFile, MultipartFile stallCodeFile, HttpServletRequest request){
		toMarket.setState(toMarketService.findById(toMarket.getId()).get().getState());
		toMarketService.update(toMarket, markCodeFile, stallCodeFile, request);
		return new MyResult(0, "修改成功");
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除信息", notes = "- 等待管理员审核")
	@DeleteMapping("/id")
	public MyResult deleteById(String id){
		ToMarket toMarket = toMarketService.findById(id).get();
		toMarket.setState(3);
		toMarketService.update(toMarket);
		return MyResult.build(0,"成功");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "批量删除信息", notes = "- 等待管理员审核 \n - 参数传递id的字符串 用英文的逗号分开")
	@DeleteMapping("/delete")
	public MyResult delete(String ids){
		toMarketService.changeBatch(3, ids);
		return new MyResult(0, "删除成功");
	}

	/**
	 * 按市场名字模糊查询
	 * @param name
	 * @return
	 */
	@ApiOperation(value = "按市场名称模糊查询", notes = "- 只查询审核通过的数据")
	@GetMapping(value="findByName")
	public MyResult findByName(String name){
		List<ToMarket> list = toMarketService.findByNameLike(name);
		return  new MyResult(0,"查询成功", list );
	}

	/**
	 * 按市场档口名字模糊查询
	 * @param stallName
	 * @return
	 */
	@ApiOperation(value = "按市场档口名称模糊查询", notes = "- 只查询审核通过的数据")
	@GetMapping(value="findByStallName")
	public MyResult findByStallName(String stallName){
		List<ToMarket> list = toMarketService.findByStallNameLike(stallName);
		return  new MyResult(0,"查询成功", list );
	}

	/**
	 * 按省返回所有市
	 * @param province
	 * @return
	 */
	@ApiOperation(value = "按省返回所有市")
	@GetMapping(value = "/findCity")
	public MyResult findCity(String province){
		List<String> list = toMarketService.findByProvince(province);
		return new MyResult(0, "成功", list);
	}

	/**
	 * 按省市返回所有区
	 * @param province
	 * @param city
	 * @return
	 */
	@ApiOperation(value = "按省市返回所有区")
	@GetMapping(value = "/findDistrict")
	public MyResult findDistrict(String province, String city){
		List<String> list = toMarketService.findByCity(province, city);
		return new MyResult(0, "成功", list);
	}

	/**
	 * 按省市区返回市场名称
	 * @param province
	 * @param city
	 * @param district
	 * @return
	 */
	@ApiOperation(value = "按省市区返回市场")
	@GetMapping(value = "/findMarket")
	public MyResult findMarket(String province, String city, String district){
		List<ToMarket> list = toMarketService.findByDistrict(province, city, district);
		return new MyResult(0, "成功", list);
	}

	@ApiOperation(value = "根据屠宰场id返回最近半年档口数")
	@GetMapping(value = "/stallsCountByshId")
	public MyResult stallsCount(String slaughterHouseId){
		ToMarketCurveVO result = toMarketService.stallsCount(slaughterHouseId);
		return new MyResult(0, "成功", result);
	}

	@ApiOperation(value = "管理员审核 - 发布通过 " , notes = "- 批量审核：参数传递id的字符串 用英文的逗号分开")
	@RequestMapping(value = "/agreePublishs", method = RequestMethod.POST)
	public MyResult agreePublishs(String ids) {
		toMarketService.changeBatch(1, ids);
		return MyResult.build(0, "成功");
	}

	@ApiOperation(value = "管理员审核 - 删除通过 " , notes = "- 批量审核：参数传递id的字符串 用英文的逗号分开")
	@RequestMapping(value = "/agreeDeleteS", method = RequestMethod.POST)
	public MyResult agreeDeleteS(String ids) {
		toMarketService.deleteBatch(ids);
		return MyResult.build(0, "成功");
	}

	/**
	 * 查询
	 * @return
	 */
	@ApiOperation(value = "发布待审核列表")
	@GetMapping("/findAgreeing")
	public MyResult findAgreeing() {
		List<ToMarket> list = toMarketService.findAll(2);
		return MyResult.build(0, "成功",  list);
	}

	/**
	 * 查询
	 * @return
	 */
	@ApiOperation(value = "删除待审核列表")
	@GetMapping("/findDeleting")
	public MyResult findDeleting() {
		List<ToMarket> list = toMarketService.findAll(3);
		return MyResult.build(0, "成功",  list);
	}

}
